﻿/*
 * @file: UsersBLL.cs
 * @in namespace: BLL
 */
using System;
using System.Data;
using DAL;

namespace BLL
{
    public class UsersBLL 
    {
        public UsersBLL()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        #region IUsers Members

        /// <summary>
        /// GetUsers
        /// </summary>
        /// <param name="allowFilter">"true" if allow ignore hidden records</param>
        /// <returns>Records corresponding</returns>
        public static Users[] GetUsers(bool allowFilter)
        {
            try
            {
                return DataAccessHelper.GetUsersDA().GetUsers(allowFilter);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// GetUserByUserName
        /// </summary>
        /// <param name="userName">User User Name</param>
        /// <param name="allowFilter">"true" if allow ignore hidden records</param>
        /// <returns>Record corresponding</returns>
        public static Users GetUserByEmail(string email, bool allowFilter)
        {
            try
            {
                return DataAccessHelper.GetUsersDA().GetUser(email, allowFilter);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// GetUserByUserID
        /// </summary>
        /// <param name="userID">User ID</param>
        /// <param name="allowFilter">"true" if allow ignore hidden records</param>
        /// <returns>Record corresponding</returns>
        public static Users GetUserByUserID(int userID, bool allowFilter)
        {
            try
            {
                return DataAccessHelper.GetUsersDA().GetUser(userID, allowFilter);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// InsertUser
        /// </summary>
        /// <param name="user">Users entity</param>
        /// <param name="autoNumber">Curent User ID inserted(output)</param>
        /// <returns>1 if successful, 0 if error occurred</returns>
        public static int InsertUser(Users user, out int autoNumber)
        {
            try
            {
                if (user.RoleID == 0)
                    throw new OnlineFloralDeliveryException(2, "RoleID is not allow null or empty");
                else if (user.Email == null || user.Email.Length == 0)
                    throw new OnlineFloralDeliveryException(2, "Email is not allow null or empty");
                else if (UsersDA.EmailExisted(user.Email) > 0)
                    throw new OnlineFloralDeliveryException(1, "Email already Existed");
                else if (user.Password == null || user.Password.Length == 0)
                    throw new OnlineFloralDeliveryException(2, "Password is not allow null or empty");
                else if (user.Password.Length < 6 || user.Password.Length > 30)
                    throw new OnlineFloralDeliveryException(3, "Password required lengths between 6 and 30");
                else if (user.FirstName == null || user.FirstName.Length == 0)
                    throw new OnlineFloralDeliveryException(2, "FirstName is not allow null or empty");
                else if (user.LastName == null || user.LastName.Length == 0)
                    throw new OnlineFloralDeliveryException(2, "LastName is not allow null or empty");
                else if (user.Gender != true && user.Gender != false)
                    throw new OnlineFloralDeliveryException(2, "Gender is not allow null or empty");
                else if (user.Address == null || user.Address.Length == 0)
                    throw new OnlineFloralDeliveryException(2, "Address is not allow null or empty");
                else if (user.Phone == null || user.Phone.Length == 0)
                    throw new OnlineFloralDeliveryException(2, "Phone is not allow null or empty");
                else if (user.SecretQuestion == null || user.SecretQuestion.Length == 0)
                    throw new OnlineFloralDeliveryException(2, "SecretQuestion is not allow null or empty");
                else if (user.SecretAnswer == null || user.SecretAnswer.Length == 0)
                    throw new OnlineFloralDeliveryException(2, "SecretAnswer is not allow null or empty");

                //Encrypt
                user.Password = Encrypt.MD5Encrypt.GetMD5Hash(user.Password);
                user.SecretAnswer = Encrypt.MD5Encrypt.GetMD5Hash(user.SecretAnswer);

                //Check after encrypt
                if (user.Password == null || user.Password.Length == 0)
                    throw new OnlineFloralDeliveryException(4, "An error occurred when encrypt the Password");
                else if (user.SecretAnswer == null || user.SecretAnswer.Length == 0)
                    throw new OnlineFloralDeliveryException(4, "An error occurred when encrypt the SecretAnswer");

                return DataAccessHelper.GetUsersDA().InsertUser(user, out autoNumber);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// UpdateUser
        /// </summary>
        /// <param name="user">Users entity</param>
        /// <returns>1 if successful, 0 if error occurred</returns>
        public static int UpdateUser(Users user)
        {
            try
            {
                if (user.UserID == 0)
                    throw new OnlineFloralDeliveryException(2, "UserID is not allow null or empty");
                else if (user.RoleID == 0)
                    throw new OnlineFloralDeliveryException(2, "RoleID is not allow null or empty");
                else if (user.Password == null || user.Password.Length == 0)
                    throw new OnlineFloralDeliveryException(2, "Password is not allow null or empty");
                else if (user.FirstName == null || user.FirstName.Length == 0)
                    throw new OnlineFloralDeliveryException(2, "FirstName is not allow null or empty");
                else if (user.LastName == null || user.LastName.Length == 0)
                    throw new OnlineFloralDeliveryException(2, "LastName is not allow null or empty");
                else if (user.Gender != true && user.Gender != false)
                    throw new OnlineFloralDeliveryException(2, "Gender is not allow null or empty");
                else if (user.Address == null || user.Address.Length == 0)
                    throw new OnlineFloralDeliveryException(2, "Address is not allow null or empty");
                else if (user.Phone == null || user.Phone.Length == 0)
                    throw new OnlineFloralDeliveryException(2, "Phone is not allow null or empty");
                else if (user.SecretQuestion == null || user.SecretQuestion.Length == 0)
                    throw new OnlineFloralDeliveryException(2, "SecretQuestion is not allow null or empty");
                else if (user.SecretAnswer == null || user.SecretAnswer.Length == 0)
                    throw new OnlineFloralDeliveryException(2, "SecretAnswer is not allow null or empty");

                //Encrypt
                user.Password = Encrypt.MD5Encrypt.GetMD5Hash(user.Password);
                user.SecretAnswer = Encrypt.MD5Encrypt.GetMD5Hash(user.SecretAnswer);

                //Check after encrypt
                if (user.Password == null || user.Password.Length == 0)
                    throw new OnlineFloralDeliveryException(4, "An error occurred when encrypt the Password");
                else if (user.SecretAnswer == null || user.SecretAnswer.Length == 0)
                    throw new OnlineFloralDeliveryException(4, "An error occurred when encrypt the SecretAnswer");

                return DataAccessHelper.GetUsersDA().UpdateUser(user);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// DisableUser
        /// </summary>
        /// <param name="userID">User ID</param>
        /// <param name="isEnable">"true" if allow enable User, "false" if allow hidden User</param>
        /// <returns>1 if successful, 0 if error occurred</returns>
        public static int DisableUser(int userID, bool isEnable)
        {
            try
            {
                return DataAccessHelper.GetUsersDA().DisableUser(userID, isEnable);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// DisableUser
        /// </summary>
        /// <param name="userName">User Name</param>
        /// <param name="isEnable">"true" if allow enable User, "false" if allow hidden User</param>
        /// <returns>1 if successful, 0 if error occurred</returns>
        public static int DisableUser(string email, bool isEnable)
        {
            try
            {
                return DataAccessHelper.GetUsersDA().DisableUser(email, isEnable);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion
    }
}
